var ctx;
var houseId;
var carBillId;
function setCtx(ctx) {
    this.ctx = ctx;
}

$(function() {
    initListener();
})
function initListener() {
    $('#houseId').change(
            function() {
                houseId = $("#houseId").val();
                if (houseId != '') {
                    $.ajax({
                        type : "GET",
                        url : ctx + "/carBill/getCarBillListByHouseId",
                        cache : false,
                        dataType : 'json',
                        data : {
                            houseId : houseId
                        },
                        success : function(data) {
                            if (data.success) {
                                var carBill = $("#carBillId");
                                carBill.empty();
                                var result = "<option value='"
                                        + "'>--请选择车辆--</option>";
                                $.each(data.obj, function(index, item) {
                                    result += "<option id='" + item.carNo + "'"
                                            + " value='" + item.id + "'>"
                                            + item.carNo + "</option>";
                                });
                                carBill.html('');
                                carBill.append(result);
                            } else {
                                var result = "<option value='"
                                        + "'>--请选择车辆--</option>";
                                carBill.html('');
                                carBill.append(result);
                            }
                        }
                    });
                }
            });

    $('#carBillId').change(function() {
        getCarBillInfo();
    });

}

function checkMonths() {
    var months = $("#months").val();
    var reg = new RegExp("^[0-9]*$");

    //解决ie9以下placeholder与value重复的问题
    if (months == $("#months").attr("placeholder")) {
        months = "";
    }

    if (months == null || months == "" || (!reg.test(months))) {
        layer.tips('请填写月数!', '#months', {
            tips : 3,
            time : 1000
        });
        return false;
    } else {
        getCarBillInfo()
        return true;
    }
}

function checkHouse(){
    var houseId = $('#houseId').val();
    if (houseId == null || houseId == "") {
        layer.tips('请选择房屋!', '#houseId', {
            tips : 3,
            time : 1000
        });
        $('#houseId').focus();
        return false;
    } else {
        return true;
    }
}

function checkCar(){
    var carBillId = $('#carBillId').val();
    if (carBillId == null || carBillId == "") {
        layer.tips('请选择车牌号!', '#carBillId', {
            tips : 3,
            time : 1000
        });
        $('#carBillId').focus();
        return false;
    } else {
        return true;
    }
}

function getCarBillInfo(){
    var months = $("#months").val();
    var reg = new RegExp("^[0-9]*$");
    //解决ie9以下placeholder与value重复的问题
    if (months == $("#months").attr("placeholder")) {
        months = "";
    }
    if (months == null || months == "" || (!reg.test(months))) {
        return false;
    }
    
    var carBillId = $('#carBillId').val();
    if (carBillId == null || carBillId == "") {
        return false;
    }
    $.ajax({
        type : "GET",
        url : ctx + "/carBill/getStartDate",
        cache : false,
        dataType : 'json',
        data : {
            houseId : houseId,
            carBillId : carBillId,
            months:months
        },
        success : function(data) {
            if (data.success) {
                $("#begainTime").html(data.obj.memo);
                $("#propertyBill").html(data.obj.unitPrice);
            } else {
                layer.msg(data.msg, {icon: 5});
            }
        }
    });
}

function payCarBill(){
    var months = $('#months').val();
    var carBillId = $('#carBillId').val();
    var houseId = $('#houseId').val();
    var billTitle = $('#billTitle').val();
    
    if (checkMonths() && checkHouse() && checkCar()) {
    	 var index = layer.load(1);
        $.post(ctx +"/carBill/saveCarBill", 
                {houseId:houseId, carBillId:carBillId, 
                   months:months, billTitle:billTitle}, function(data) {
            data = JSON.parse(data);
            if (data.success) {
                layer.open({
                    type: 2,
                    title: '停车费',
                    content: ctx + "/pay/toPayBillPage?billId=" + data.obj + "&type=3",
                    skin: "house-add",
                    area: ["600px","480px"],
                    end: function(){
                    	layer.close(index);
                    }
                });
            } else {
                layer.msg(data.msg, {
                    icon : 2,
                    time : 1000
                // 2秒关闭（如果不配置，默认是3秒）
                }, function() {
                	layer.close(index);
                    parent.layer.close(index); // 根据窗口索引关闭窗口
                });
            }
        });
    }
}
